package cn.newtouch.system.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.system.domain.Permission;
import cn.newtouch.system.query.PermissionQuery;
import cn.newtouch.system.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(value = "权限的API",description="权限相关的CRUD功能")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有权限" )
    @PreAuthorize(name = "所有权限",sn = "permission:getAll")
    public List<Permission> findAll(){
        return permissionService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询权限",sn = "permission:get")
    public Permission findOne(@PathVariable Long id){
       return permissionService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "通过ID删除权限",sn = "permission:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            permissionService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存权限",sn = "permission:save")
    public JsonResult updateOrAdd(@RequestBody Permission permission){
        try {
            if(permission.getId()==null){
                //添加操作
                permissionService.save(permission);
            }else{
                //修改操作
                permissionService.update(permission);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页查询权限",sn = "permission:list")
    public PageList<Permission> queryPage(@RequestBody PermissionQuery query){
        try {
            return permissionService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除权限",sn = "permission:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            permissionService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

}
